
package com.example.mvc.controller;

import com.example.mvc.entities.User;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Locale;

/**
 * 映射请求参数
 *  查询参数、路径参数、矩阵参数
 * 映射请求头
 *  常用请求头
 * 映射cookie
 *  cookie
 */
@Controller
public class ParamsController {

    static final String SUCCESS = "success";
    Logger log = LoggerFactory.getLogger(ParamsController.class);

    // 查询参数
    @RequestMapping(path = "/testQuery")
    public String testQuery(@RequestParam("username") String username,
                            @RequestParam(value = "age", required = false, defaultValue = "0") int age) {
        log.info("testQuery:{},{}", username, age);
        return SUCCESS;
    }

    // 路径参数
    @RequestMapping(path = "/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") Integer id) {
        log.info("testPathVariable:{}", id);
        return SUCCESS;
    }

    // 请求头
    @RequestMapping(path = "/testHeader")
    public String testHeader(@RequestHeader("Accept-Language") String language) {
        log.info("testHeader:{}", language);
        return SUCCESS;
    }

    // cookie
    @RequestMapping(path = "/testCookie")
    public String testCookie(@CookieValue("JSESSIONID") String sessionId,
                             @CookieValue("JSESSIONID") Cookie cookie) {
        log.info("testCookie:{}", sessionId);
        log.info("testCookie:{}", cookie);
        return SUCCESS;
    }


    @RequestMapping(path = "/testPojo")
    public String testPojo( User user) {
        log.info("testPojo:{}", user);
        return SUCCESS;
    }

    @RequestMapping(path = "/servletAPI")
    public String servletAPI(HttpServletRequest req, HttpServletResponse resp,
                             Locale locale, Reader reader, Writer writer ) {
        return SUCCESS;
    }
}